package com.example.mytestdemo.linearlayoutmanager

import android.annotation.SuppressLint
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRotationLayoutBinding
import com.example.mytestdemo.utils.ScreenUtils

/**
 *参考：https://blog.csdn.net/u011387817/article/details/80313184
 * https://blog.csdn.net/AliEnCheng/article/details/122541863
 */
class CustomRotationActivity :
    BaseViewBindActivity<ActivityLinearinglayoutManagerRotationLayoutBinding>() {
    lateinit var toast: Toast

    private var mArcSlidingHelper: ArcSlidingHelper? = null
    private lateinit var mView: View

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_linearinglayout_manager_rotation_layout
    }

    override fun initData() {
    }

    override fun initUI() {
        mView = mBind.llDrag
        toast = Toast.makeText(this, "aa", Toast.LENGTH_SHORT)
        mView.post{
            mArcSlidingHelper = ArcSlidingHelper().create(mView,object :OnSlidingListener{
                override fun onSliding(angle: Float) {
                    mView.setRotation(mView.getRotation() + angle)
                }

            })
        }
        getWindow().getDecorView().setOnTouchListener(object :View.OnTouchListener{
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                //处理滑动事件
                mArcSlidingHelper?.handleMovement(event)
                return true
            }

        })

//        touch()

    }

    fun touch() {
        mView.setOnTouchListener(object : View.OnTouchListener {
            var downX = 0
            var downY = 0

            var moveX = 0
            var moveY = 0
            var left = mView.left
            var top = mView.top
            var right = mView.right
            var bottom = mView.bottom

            @SuppressLint("ClickableViewAccessibility")
            override fun onTouch(v: View?, event: MotionEvent): Boolean {

                if (event.action == MotionEvent.ACTION_DOWN) {
                    downX = event.rawX.toInt()
                    downY = event.rawY.toInt()
                    Log.d(TAG, "tgw ACTION_DOWN: downX----:$downX--------downY:$downY")
                    Log.d(
                        TAG,
                        "tgw ACTION_DOWN: view layout----left:${mView.left}----right:${mView.right}----top:${mView.top}----bottom:${mView.bottom}"
                    )
                    Log.d(
                        TAG,
                        "tgw ACTION_DOWN: view layout----getScreenHeight:${
                            ScreenUtils.getScreenHeight(this@CustomRotationActivity)
                        }"
                    )
                    Log.d(
                        TAG,
                        "tgw ACTION_DOWN: view layout----getScreenWidth:${
                            ScreenUtils.getScreenWidth(this@CustomRotationActivity)
                        }"
                    )
                    left = mView.left
                    top = mView.top
                    right = mView.right
                    bottom = mView.bottom

                } else if (event.action == MotionEvent.ACTION_MOVE) {
                    Log.d(TAG, "tgw onTouch: downX----:$downX--------downY:$downY")
                    Log.d(
                        TAG,
                        "tgw onTouch: event.rawX.toInt()----:${event.rawX.toInt()}--------event.rawY.toInt():${event.rawY.toInt()}"
                    )
                    Log.d(
                        TAG,
                        "tgw onTouch: event.x.toInt()----:${event.x.toInt()}--------event.y.toInt():${event.y.toInt()}"
                    )
                    moveX = event.rawX.toInt() - downX
                    moveY = event.rawY.toInt() - downY
                    Log.d(TAG, "tgw onTouch: moveX----:$moveX--------moveY:$moveY")
                    Log.d(
                        TAG,
                        "tgw onTouch: 初始的 view layout----left:${left}----right:${right}----top:${top}----bottom:${bottom}"
                    )
                    Log.d(
                        TAG,
                        "tgw onTouch: 变化的 view layout----left:${mView.left}----right:${mView.right}----top:${mView.top}----bottom:${mView.bottom}"
                    )
                    //位置移动但是 layout 也变化
//                    if (left+moveX>=0 && top+moveY>=0 && right+moveX<=ScreenUtils.getScreenWidth(this@CustomRotationActivity) && bottom+moveY<=ScreenUtils.getScreenHeight(this@CustomRotationActivity)-100){
//                        mView.layout(left+moveX,top+moveY,right+moveX,bottom+moveY)
//                    }

                    //位置移动但是 layout 不变
                    if (left + moveX >= 0 && top + moveY >= 0 && right + moveX <= ScreenUtils.getScreenWidth(
                            this@CustomRotationActivity
                        ) && bottom + moveY <= ScreenUtils.getScreenHeight(this@CustomRotationActivity) - 100
                    ) {
                        mView.translationX = moveX.toFloat()
                        mView.translationY = moveY.toFloat()
                    }

                }




                return true
            }
        })

    }
}